package com.changgou.user.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.user.config.TokenDecode;
import com.changgou.user.service.AddressService;
import com.changgou.user.pojo.Address;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/address")
public class AddressController {


    @Autowired
    private AddressService addressService;


    @Autowired
    private TokenDecode tokenDecode;


    /**
     * @return java.util.List<com.changgou.user.pojo.Address>
     * @description: 根据登录名字得到收件人地址信息
     * @author 大佬味的小男孩
     * @date 2020/08/02 20:49
     */
    @GetMapping("list")
    public Result<List<Address>> list() {
        String username = tokenDecode.getUserInfo().get("username");
        List<Address> addressList = addressService.list(username);
        return new Result<>(true, StatusCode.OK, "查询收件人地址成功", addressList);
    }

    //查询全部数据
    @GetMapping
    public Result findAll() {
        List<Address> addressList = addressService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", addressList);
    }

    //根据ID查询数据
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Address address = addressService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", address);
    }

    //新增数据
    @PostMapping
    public Result add(@RequestBody Address address) {
        addressService.add(address);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    //修改数据
    @PutMapping(value = "/{id}")
    public Result update(@RequestBody Address address, @PathVariable Integer id) {
        address.setId(id);
        addressService.update(address);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    //根据ID删除
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id) {
        addressService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }


}
